package com.imooc.ad.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import java.util.logging.Filter;

/*
 *Created by shan
 */
@Slf4j
@Component
public class PreRequestFilter extends ZuulFilter {
    /**定义filter的类型（执行时间），四种，pre,Routing,post,error*/
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }
    /**定义执行的顺序，数值越小，执行的优先级越高*/
    @Override
    public int filterOrder() {
        return 0;
    }
    /**定义当某些条件发生的时候，在执行*/
    @Override
    public boolean shouldFilter() {
        return true;
    }
    /**定义filter执行的操作*/
    @Override
    public Object run() throws ZuulException {
        //请求上下文  RequestContext
        RequestContext ctx  = RequestContext.getCurrentContext();
        //记录当前的时间戳
        ctx.set("startTime",System.currentTimeMillis());
        return null;
    }
}
